if orders and not orders.NewTransaction then


dofile (ScriptsRootPath .. "lib\\orders.transaction.limit.lua")
dofile (ScriptsRootPath .. "lib\\orders.transaction.stop.lua")


function orders:NewTransaction(template, addition)
	if not template then return end
	
	local transaction = {
		ACTION = template.ACTION,
		OPERATION = template.side__ or template.OPERATION,
		--QUANTITY = tostring((template.qty__ and int(template.qty__) or (template.qty and int(template.qty)) or template.QUANTITY)),
		QUANTITY = tostring((template.ex__ and template.ex__.qty and int(template.ex__.qty))
							or (template.qty__ and int(template.qty__))
							or (template.balance and int(template.balance))
							or (template.qty and int(template.qty))
							or template.QUANTITY
					),
		EXPIRY_DATE = "TODAY",
		SECCODE = template.code or template.sec_code or template.SECCODE,
		CLASSCODE = template.class_code or template.CLASSCODE,
		CLIENT_CODE = template.client_code or template.CLIENT_CODE or lib:GetClientCode(),
		ACCOUNT = template.account or template.ACCOUNT or lib:GetAccount(template.CLASSCODE or template.class_code),
				
		ex__ = template.ex__ or {}
	}
	
	if addition then
		for k, v in pairs(addition) do
			if k ~= "ex__" then
				transaction[k] = v
			end
		end
		
		if addition.ex__ then
			for k, v in pairs(addition.ex__) do
				transaction.ex__[k] = v
			end
		end
	end
	
	for k, v in pairs(transaction) do
		if v == false or v == "false" then
			transaction[k] = nil
		end
	end
		
	return transaction
end


function orders:sendTransaction(transaction, ...)
	--if type(transaction) ~= "table" then return transaction end
	if type(transaction) ~= "table" then return end
		
	local next_transaction = {...}		-- array of next transactions
	
	if not transaction.SECCODE then		-- transaction is array of transactions
		if #transaction <= 0 then return end
		
		next_transaction = transaction
		transaction = transaction[1]
		table.remove(next_transaction, 1)
	end
	
	if #next_transaction > 0 then
		transaction.next_transaction__ = self:TransactionChain(next_transaction)
	end
	
	return self:sendQueue(transaction)
	
	--[[
	local result = sendTransaction(lib:clone(transaction, nil, {string = true}))
	
	if result == "" then
		self.transactions[transaction.TRANS_ID] = transaction
		orders:lock_queue()
		orders.transactions.count = orders.transactions.count + 1
		self.log("sendTransaction", transaction)
		self.log("Transaction Queue", self.transactions)
		return transaction
	else
		self:log(fail("sendTransaction failed: " .. tostring(result)), transaction)
	end
	--]]
end


function orders:sendQueue(transaction)
	if transaction
		and not self:IsNewLimit(transaction)
		and not self:IsNewStop(transaction)
		and (self.transactions.count > 0 or self.orders.count > 0)
	then
	
		table.insert(self.queue, transaction)
		message("sendTransaction queued: #" .. tostring(#self.queue) .. " " .. tostring(transaction.SECCODE) .. " " .. tostring(transaction.ACTION))
		return transaction
		
	elseif not transaction
		and self.transactions.count == 0 and self.orders.count == 0
		and #self.queue > 0
	then
	
		transaction = self.queue[1]
		table.remove(self.queue, 1)
		
	end
	
	return self:sendNext(transaction)
end


function orders:sendNext(transaction)
	if transaction then
		local i = 0
		repeat
			i = i + 1
			transaction.TRANS_ID = tostring(lib:tickseconds()) .. tostring(i)
		until(not self.transactions[transaction.TRANS_ID])
		
		local result = sendTransaction(lib:clone(transaction, nil, {string = true}))
		
		if result == "" then
			if transaction.ex__ then
				transaction.ex__.date = lib:FormatDate()
				transaction.ex__.time = lib:ticktime()
			end
	
			self.transactions[transaction.TRANS_ID] = transaction
			orders:lock_queue()
			orders.transactions.count = orders.transactions.count + 1
			
			lib:SubscribeLevel2(transaction.SECCODE, transaction.CLASSCODE)
			
			message("sendTransaction: " .. tostring(transaction.TRANS_ID))
			self.log("sendTransaction", transaction)
			self.log("Transaction Queue", self.transactions)
			
			return transaction
		else
			self:log(fail("sendTransaction failed: " .. tostring(result)), transaction)
		end
	end
end


function orders:TransactionChain(transaction, ...)
	if type(transaction) ~= "table" then return end
	
	local next_transaction = {...}		-- array of next transactions
	
	if not transaction.SECCODE then		-- transaction is array of transactions
		if #transaction <= 0 then return end
		
		next_transaction = transaction
		transaction = transaction[1]
		table.remove(next_transaction, 1)
	end
	
	local last = transaction
	
	for _, v in ipairs(next_transaction) do
		if type(v) ~= "table" then break end
		last.next_transaction__ = v
		last = v
	end
	
	return transaction
end


end -- of file
